﻿using System;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using SharePointListSecurity.Model;


namespace SharePointListSecurity.WebControls
{
    public class CustomViewSelector : ViewSelectorMenu
    {
        #region overridenMethods
        protected override void OnLoad(EventArgs e)
        {
            if (this.Visible)
            {
                SPWeb web = SPContext.Current.List.ParentWeb;
                var sharepointListSecurityService = new SharepointListSecurityService();
                var redirectToDefaultView = sharepointListSecurityService.IsOnlyDefaultViewPermissed(SPContext.Current.List);
                if (!base.RenderContext.ViewContext.View.DefaultView && redirectToDefaultView)
                    SPUtility.Redirect(SPContext.Current.List.DefaultView.ServerRelativeUrl, SPRedirectFlags.Default, HttpContext.Current, "redirect=true");
            }
            base.OnLoad(e);
        }
        #endregion
    }
}